package com.cskaoyan.controller;

import com.cskaoyan.bean.OrderGoods;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.wxAuth.bo.WxAuthCommentBo;
import com.cskaoyan.bean.wxorder.bo.WxOrderSubmitBo;
import com.cskaoyan.bean.wxorder.vo.WxOrderDetailVo;
import com.cskaoyan.bean.wxorder.vo.WxOrderListVo;
import com.cskaoyan.service.wxorder.WxOrderService;
import com.cskaoyan.bean.wxAuth.bo.WxAuthCommentBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @RequestMapping("list")
    public BaseRespVo list(Integer showType, Integer page, Integer size) {
        WxOrderListVo result = wxOrderService.list(showType, page, size);
        return BaseRespVo.ok(result);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WxOrderDetailVo result = wxOrderService.getOrderDetail(orderId);
        return BaseRespVo.ok(result);
    }
    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId){
        OrderGoods result = wxOrderService.goods(orderId, goodsId);
        return BaseRespVo.ok(result);
    }

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody WxAuthCommentBo comment){
        Integer result = wxOrderService.addComment(comment);
        return result == 1 ? BaseRespVo.ok(): BaseRespVo.fail();
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map<String,Integer> orderId){
        Integer id = orderId.get("orderId");
        Integer result = wxOrderService.confirmForOrder(id);
        return BaseRespVo.ok();
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map orderId) {
        Integer orderId1 = (Integer) orderId.get("orderId");
        Integer result = wxOrderService.cancelOrderByOrderId(orderId1);
        return result == 1 ? BaseRespVo.ok() : BaseRespVo.fail();
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map<String,Integer> orderId){
        /*Integer id = orderId.get("orderId");
        Integer result = null;
        if (id % 2 == 1) {
            result = wxOrderService.perpay(id);
            result = id % 2 ;
        }
        result = id %2;*/
        return BaseRespVo.fail(724, "付款失败");
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map<String,Integer> orderId) {
        Integer id = orderId.get("orderId");
        Integer result = wxOrderService.deleteOrderById(id);
        return result == 1 ? BaseRespVo.ok() : BaseRespVo.fail();
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map<String,Integer> orderId) {
        Integer id = orderId.get("orderId");
        Integer result = wxOrderService.refundOrderById(id);
        return result == 1 ? BaseRespVo.ok() : BaseRespVo.fail();
    }

    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody WxOrderSubmitBo submitPerm){
        /**
         * result为200时，代表添加成功
         * result为404时，代表团购活动过期
         */
        Integer result = wxOrderService.addOrder(submitPerm);
        Map<String, Integer> stringIntegerMap = new HashMap<>();
        stringIntegerMap.put("orderId", result);
        return BaseRespVo.ok(stringIntegerMap);
    }
}
